﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WordFun.Models;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace WordFun.Utils
{
    public sealed partial class ControlChar : UserControl
    {
        #region Static and const variable
        static int _beginTime = 0;
        const int DELAY_STEP = 1000000;
        #endregion

        #region private variable
        ControlCharStates _currentState, _prevState; // current state of ControlWord object
        #endregion

       /// <summary>
       /// Contructor create default ControlWord
       /// </summary>
        public ControlChar()
        {
            this.InitializeComponent(); // init component of GUI
            this.Initialize(); //init some default value
            this.InitializeAnimation(); //init animation
        }

        #region Public Method
        /// <summary>
        /// Return string content of this control
        /// </summary>
        public string CharContent
        {
            get
            {
                return textBlock.Text;
            }

            set
            {
                textBlock.Text = value;
            }
        }

        /// <summary>
        /// return current state
        /// </summary>
        /// <returns>CotrolWordStates enum</returns>
        public ControlCharStates GetCurrentState()
        {
            return _currentState;
        }

        /// <summary>
        /// Set current state of ControlWord
        /// </summary>
        /// <param name="state">Next state</param>
        public void SetState(ControlCharStates state)
        {
            _prevState = _currentState;
            _currentState = state;
            switch (state)
            {
                case ControlCharStates.Normal: SetNormalState();
                    break;
                case ControlCharStates.Selecting: SetSelectingState();
                    break;
                case ControlCharStates.SwappingDown: SetSwappingDownState();
                    break;
                case ControlCharStates.SwappingUp: SetSwappingUpState();
                    break;
            }
        }

        /// <summary>
        /// Set object current location
        /// </summary>
        /// <param name="point">(Left, Top)</param>
        public void SetLocation(Point2D point)
        {
            this.Margin = new Thickness(point.X, point.Y, 0, 0);
        }
        #endregion

        #region set ControlWord state
        /// <summary>
        /// Set current state to Swapping Up State
        /// </summary>
        private void SetSwappingUpState()
        {
            SetupAnimation(); // set swapping up animation
        }

        /// <summary>
        /// Set current state to Swapping Down State
        /// </summary>
        private void SetSwappingDownState()
        {
            SetupAnimation(); // set swapping down animation
        }

        /// <summary>
        /// Set current state to selecting state
        /// </summary>
        private void SetSelectingState()
        {
            SetupAnimation(); // set animation
        }

        /// <summary>
        /// Set current state to normal state
        /// </summary>
        private void SetNormalState()
        {
            SetupAnimation(); // set animation
            RefreshBeginTime();// get another time delay
        }

        /// <summary>
        /// Set animation for currentState
        /// </summary>
        /// <param name="state">ControlWordStates enum</param>
        private void SetupAnimation()
        {
            //convert ControlWordStates to integer value
            int idAnimation = (int)_currentState;
            int idPrev = (int)_prevState;
   
            WordAnimation.States[idPrev].Storyboard.Stop();//Stop previous state
            WordAnimation.States[idAnimation].Storyboard.Begin(); // active animation
        }

        /// <summary>
        /// Change delay time of animation
        /// </summary>
        private void RefreshBeginTime()
        {
            if (_beginTime > Constant.MAX_INT)
            {
                _beginTime = 0;
            }
            else
            {
                _beginTime += DELAY_STEP;
            }
        }
        #endregion

        #region Event 
        /// <summary>
        /// Tapped Event_ change current state to Selecting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            if (_currentState != ControlCharStates.Selecting)
            {
                SetState(ControlCharStates.Selecting); // set current state to selecting
            }
            else
            {
                SetState(ControlCharStates.Normal); // set current state to normal, release selected            
            }
        }
        #endregion

        #region Initialize Method
        /// <summary>
        /// Init some default value
        /// </summary>
        private void Initialize()
        {
            WordAnimation.States[(int)ControlCharStates.Normal].Storyboard.BeginTime = new TimeSpan(_beginTime); //Delay normal state time
            SetState(ControlCharStates.Normal); // set default state to nomral state
        }

        /// <summary>
        /// Init animation
        /// </summary>
        private void InitializeAnimation() 
        {
            WordAnimation.States[(int)ControlCharStates.Normal].Storyboard.RepeatBehavior = Windows.UI.Xaml.Media.Animation.RepeatBehavior.Forever;
            WordAnimation.States[(int)ControlCharStates.Selecting].Storyboard.RepeatBehavior = Windows.UI.Xaml.Media.Animation.RepeatBehavior.Forever;

            WordAnimation.States[(int)ControlCharStates.SwappingDown].Storyboard.Completed +=Storyboard_SwappingDown_Completed;
            WordAnimation.States[(int)ControlCharStates.SwappingUp].Storyboard.Completed +=Storyboard_SwappinpUp_Completed;
        }

        private void Storyboard_SwappinpUp_Completed(object sender, object e)
        {
            SetState(ControlCharStates.Normal);
        }

        private void Storyboard_SwappingDown_Completed(object sender, object e)
        {
            SetState(ControlCharStates.SwappingUp);
        }
        #endregion

    }// end ControlWord

    /// <summary>
    /// Collection of Word states
    /// </summary>
    public enum ControlCharStates
    { 
        Normal = 0, 
        Selecting = 1, 
        SwappingDown = 2, 
        SwappingUp = 3
    }
}

